package com.zxt.recruit.model;


import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * 
 * @TableName sys_element_userinfo
 */
@TableName(value ="sys_element_userinfo")
@Data
public class SysElementUserinfo implements Serializable,UserDetails {
    /**
     * 用户pk（uuid 36）
     */
    @TableId
    private String userPk;

    /**
     * 用户登录账号
     */
    private String username;

    /**
     * 登录密码（加密共计60）
     */
    private String password;

    /**
     * 昵称
     */
    private String name;

    /**
     * 电话
     */
    private String tel;

    /**
     * 微信号（6-20）
     */
    private String wx;

    /**
     * 邮箱地址
     */
    private String mail;

    /**
     * 特长
     */
    private String talents;

    /**
     * 是否在线
     */
    private Integer isOnline;

    /**
     * 求职意向
     */
    private String jobIntention;

    /**
     * 个人简介
     */
    private String introduced;

    /**
     * 博客
     */
    private String blog;

    /**
     * 微博
     */
    private String weibo;

    /**
     * 领英
     */
    private String linkedin;

    /**
     * 年龄
     */
    private Integer age;

    /**
     * 职务
     */
    private String job;

    /**
     * 公司
     */
    private String companys;

    /**
     * 性别
     */
    private String sexs;

    /**
     * 出生日期
     */
    private LocalDateTime bornDate;

    /**
     * 省份
     */
    private String province;

    /**
     * 城市
     */
    private String city;

    /**
     * 国家
     */
    private String country;

    /**
     * 用户头像url
     */
    private String headimgurl;

    /**
     * 用户特权信息
     */
    private String privilege;

    /**
     * 其他凭证key
     */
    private String otherSecurityId;

    /**
     * 微信id
     */
    private String wxOpenId;

    /**
     * 钉钉id
     */
    private String dingdingOpenId;

    /**
     * qq凭证id
     */
    private String qqOpenId;

    /**
     * 微信公众号id
     */
    private String wxUnionOpenId;

    /**
     * gitee_id
     */
    private String giteeOpenId;

    /**
     * 微信小程序凭证openid
     */
    private String wxAppletOpenId;

    /**
     * 是否删除
     */
    private Integer isDeleted;

    /**
     * 是否被禁用
     */
    private Integer isEnabled;

    /**
     * 创建时间
     */
    private LocalDateTime inputTime;

    /**
     * 更新时间
     */
    private LocalDateTime updateTime;

    /**
     * 角色-虚拟，这个仅仅作为一个关联展示
     */
    private String roles;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    @Override
    public boolean equals(Object that) {
        if (this == that) {
            return true;
        }
        if (that == null) {
            return false;
        }
        if (getClass() != that.getClass()) {
            return false;
        }
        SysElementUserinfo other = (SysElementUserinfo) that;
        return (this.getUserPk() == null ? other.getUserPk() == null : this.getUserPk().equals(other.getUserPk()))
            && (this.getUsername() == null ? other.getUsername() == null : this.getUsername().equals(other.getUsername()))
            && (this.getPassword() == null ? other.getPassword() == null : this.getPassword().equals(other.getPassword()))
            && (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName()))
            && (this.getTel() == null ? other.getTel() == null : this.getTel().equals(other.getTel()))
            && (this.getWx() == null ? other.getWx() == null : this.getWx().equals(other.getWx()))
            && (this.getMail() == null ? other.getMail() == null : this.getMail().equals(other.getMail()))
            && (this.getTalents() == null ? other.getTalents() == null : this.getTalents().equals(other.getTalents()))
            && (this.getIsOnline() == null ? other.getIsOnline() == null : this.getIsOnline().equals(other.getIsOnline()))
            && (this.getJobIntention() == null ? other.getJobIntention() == null : this.getJobIntention().equals(other.getJobIntention()))
            && (this.getIntroduced() == null ? other.getIntroduced() == null : this.getIntroduced().equals(other.getIntroduced()))
            && (this.getBlog() == null ? other.getBlog() == null : this.getBlog().equals(other.getBlog()))
            && (this.getWeibo() == null ? other.getWeibo() == null : this.getWeibo().equals(other.getWeibo()))
            && (this.getLinkedin() == null ? other.getLinkedin() == null : this.getLinkedin().equals(other.getLinkedin()))
            && (this.getAge() == null ? other.getAge() == null : this.getAge().equals(other.getAge()))
            && (this.getJob() == null ? other.getJob() == null : this.getJob().equals(other.getJob()))
            && (this.getCompanys() == null ? other.getCompanys() == null : this.getCompanys().equals(other.getCompanys()))
            && (this.getSexs() == null ? other.getSexs() == null : this.getSexs().equals(other.getSexs()))
            && (this.getBornDate() == null ? other.getBornDate() == null : this.getBornDate().equals(other.getBornDate()))
            && (this.getProvince() == null ? other.getProvince() == null : this.getProvince().equals(other.getProvince()))
            && (this.getCity() == null ? other.getCity() == null : this.getCity().equals(other.getCity()))
            && (this.getCountry() == null ? other.getCountry() == null : this.getCountry().equals(other.getCountry()))
            && (this.getHeadimgurl() == null ? other.getHeadimgurl() == null : this.getHeadimgurl().equals(other.getHeadimgurl()))
            && (this.getPrivilege() == null ? other.getPrivilege() == null : this.getPrivilege().equals(other.getPrivilege()))
            && (this.getOtherSecurityId() == null ? other.getOtherSecurityId() == null : this.getOtherSecurityId().equals(other.getOtherSecurityId()))
            && (this.getWxOpenId() == null ? other.getWxOpenId() == null : this.getWxOpenId().equals(other.getWxOpenId()))
            && (this.getDingdingOpenId() == null ? other.getDingdingOpenId() == null : this.getDingdingOpenId().equals(other.getDingdingOpenId()))
            && (this.getQqOpenId() == null ? other.getQqOpenId() == null : this.getQqOpenId().equals(other.getQqOpenId()))
            && (this.getWxUnionOpenId() == null ? other.getWxUnionOpenId() == null : this.getWxUnionOpenId().equals(other.getWxUnionOpenId()))
            && (this.getGiteeOpenId() == null ? other.getGiteeOpenId() == null : this.getGiteeOpenId().equals(other.getGiteeOpenId()))
            && (this.getWxAppletOpenId() == null ? other.getWxAppletOpenId() == null : this.getWxAppletOpenId().equals(other.getWxAppletOpenId()))
            && (this.getIsDeleted() == null ? other.getIsDeleted() == null : this.getIsDeleted().equals(other.getIsDeleted()))
            && (this.getIsEnabled() == null ? other.getIsEnabled() == null : this.getIsEnabled().equals(other.getIsEnabled()))
            && (this.getInputTime() == null ? other.getInputTime() == null : this.getInputTime().equals(other.getInputTime()))
            && (this.getUpdateTime() == null ? other.getUpdateTime() == null : this.getUpdateTime().equals(other.getUpdateTime()))
            && (this.getRoles() == null ? other.getRoles() == null : this.getRoles().equals(other.getRoles()));
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((getUserPk() == null) ? 0 : getUserPk().hashCode());
        result = prime * result + ((getUsername() == null) ? 0 : getUsername().hashCode());
        result = prime * result + ((getPassword() == null) ? 0 : getPassword().hashCode());
        result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
        result = prime * result + ((getTel() == null) ? 0 : getTel().hashCode());
        result = prime * result + ((getWx() == null) ? 0 : getWx().hashCode());
        result = prime * result + ((getMail() == null) ? 0 : getMail().hashCode());
        result = prime * result + ((getTalents() == null) ? 0 : getTalents().hashCode());
        result = prime * result + ((getIsOnline() == null) ? 0 : getIsOnline().hashCode());
        result = prime * result + ((getJobIntention() == null) ? 0 : getJobIntention().hashCode());
        result = prime * result + ((getIntroduced() == null) ? 0 : getIntroduced().hashCode());
        result = prime * result + ((getBlog() == null) ? 0 : getBlog().hashCode());
        result = prime * result + ((getWeibo() == null) ? 0 : getWeibo().hashCode());
        result = prime * result + ((getLinkedin() == null) ? 0 : getLinkedin().hashCode());
        result = prime * result + ((getAge() == null) ? 0 : getAge().hashCode());
        result = prime * result + ((getJob() == null) ? 0 : getJob().hashCode());
        result = prime * result + ((getCompanys() == null) ? 0 : getCompanys().hashCode());
        result = prime * result + ((getSexs() == null) ? 0 : getSexs().hashCode());
        result = prime * result + ((getBornDate() == null) ? 0 : getBornDate().hashCode());
        result = prime * result + ((getProvince() == null) ? 0 : getProvince().hashCode());
        result = prime * result + ((getCity() == null) ? 0 : getCity().hashCode());
        result = prime * result + ((getCountry() == null) ? 0 : getCountry().hashCode());
        result = prime * result + ((getHeadimgurl() == null) ? 0 : getHeadimgurl().hashCode());
        result = prime * result + ((getPrivilege() == null) ? 0 : getPrivilege().hashCode());
        result = prime * result + ((getOtherSecurityId() == null) ? 0 : getOtherSecurityId().hashCode());
        result = prime * result + ((getWxOpenId() == null) ? 0 : getWxOpenId().hashCode());
        result = prime * result + ((getDingdingOpenId() == null) ? 0 : getDingdingOpenId().hashCode());
        result = prime * result + ((getQqOpenId() == null) ? 0 : getQqOpenId().hashCode());
        result = prime * result + ((getWxUnionOpenId() == null) ? 0 : getWxUnionOpenId().hashCode());
        result = prime * result + ((getGiteeOpenId() == null) ? 0 : getGiteeOpenId().hashCode());
        result = prime * result + ((getWxAppletOpenId() == null) ? 0 : getWxAppletOpenId().hashCode());
        result = prime * result + ((getIsDeleted() == null) ? 0 : getIsDeleted().hashCode());
        result = prime * result + ((getIsEnabled() == null) ? 0 : getIsEnabled().hashCode());
        result = prime * result + ((getInputTime() == null) ? 0 : getInputTime().hashCode());
        result = prime * result + ((getUpdateTime() == null) ? 0 : getUpdateTime().hashCode());
        result = prime * result + ((getRoles() == null) ? 0 : getRoles().hashCode());
        return result;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", userPk=").append(userPk);
        sb.append(", username=").append(username);
        sb.append(", password=").append(password);
        sb.append(", name=").append(name);
        sb.append(", tel=").append(tel);
        sb.append(", wx=").append(wx);
        sb.append(", mail=").append(mail);
        sb.append(", talents=").append(talents);
        sb.append(", isOnline=").append(isOnline);
        sb.append(", jobIntention=").append(jobIntention);
        sb.append(", introduced=").append(introduced);
        sb.append(", blog=").append(blog);
        sb.append(", weibo=").append(weibo);
        sb.append(", linkedin=").append(linkedin);
        sb.append(", age=").append(age);
        sb.append(", job=").append(job);
        sb.append(", companys=").append(companys);
        sb.append(", sexs=").append(sexs);
        sb.append(", bornDate=").append(bornDate);
        sb.append(", province=").append(province);
        sb.append(", city=").append(city);
        sb.append(", country=").append(country);
        sb.append(", headimgurl=").append(headimgurl);
        sb.append(", privilege=").append(privilege);
        sb.append(", otherSecurityId=").append(otherSecurityId);
        sb.append(", wxOpenId=").append(wxOpenId);
        sb.append(", dingdingOpenId=").append(dingdingOpenId);
        sb.append(", qqOpenId=").append(qqOpenId);
        sb.append(", wxUnionOpenId=").append(wxUnionOpenId);
        sb.append(", giteeOpenId=").append(giteeOpenId);
        sb.append(", wxAppletOpenId=").append(wxAppletOpenId);
        sb.append(", isDeleted=").append(isDeleted);
        sb.append(", isEnabled=").append(isEnabled);
        sb.append(", inputTime=").append(inputTime);
        sb.append(", updateTime=").append(updateTime);
        sb.append(", roles=").append(roles);
        sb.append(", serialVersionUID=").append(serialVersionUID);
        sb.append("]");
        return sb.toString();
    }

    /**
     * 进行角色权限列表获取
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 获取 roles 字段，格式可能是 [admin, user]
        String rolesString = this.getRoles();

        // 如果 rolesString 为空，返回空集合
        if (rolesString == null || rolesString.isEmpty()) {
            return List.of();
        }

        // 去除首尾的 [ ]，然后分割成列表
        List<String> rolesList = Arrays.stream(rolesString.replaceAll("[\\[\\]]", "").split(","))
                .map(String::trim)  // 去除空格
                .toList();

        // 转换为 SimpleGrantedAuthority
        return rolesList.stream()
                .map(SimpleGrantedAuthority::new)
                .toList();
    }

    @Override
    public boolean isAccountNonExpired() {
        return UserDetails.super.isAccountNonExpired();
    }

    @Override
    public boolean isAccountNonLocked() {
        return UserDetails.super.isAccountNonLocked();
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return UserDetails.super.isCredentialsNonExpired();
    }

    @Override
    public boolean isEnabled() {
        return UserDetails.super.isEnabled();
    }
}